package com.tripspins.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.tripspins.app.dao.UserDAO;
import com.tripspins.app.helper.DbHelper;
import com.tripspins.app.helper.SyncHelper;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.ProfilePictureView;


/**
 * Created by João.Rutkoski on 10/02/14.
 */
public class LoginActivity extends Activity {


    private UiLifecycleHelper uiHelper;
    LoginButton loginButton;
    DbHelper dbHelper;
    ProfilePictureView profilePictureView;
    GlobalState global;
    Button btnGoToMyTrips;
    final Context context = this;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getActionBar().hide();
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
        global = ((GlobalState)getApplicationContext());
        dbHelper = new DbHelper(this);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
        btnGoToMyTrips = (Button)findViewById(R.id.main_btn_my_trips);
        btnGoToMyTrips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent listTripsActivity = new Intent(context, MainActivity.class);
                context.startActivity(listTripsActivity);
            }
        });
        loginButton = (LoginButton) findViewById(R.id.facebook_auth_button);
        loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
            @Override
            public void onUserInfoFetched(GraphUser user) {
                if(user != null){
                    global.setLoggedFacebookUser(user);
                    profilePictureView.setProfileId(user.getId());
                    persistUser();
                    //redirect();
                }else{
                    persistLogout();
                }
            }
        });
    }

    private void persistLogout() {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        UserDAO userDAO = new UserDAO(database);
        userDAO.persistUserLogout();
        database.close();

    }


    private void persistUser() {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        UserDAO userDAO = new UserDAO(database);
        global.setLoggedLocalUser(userDAO.persist(global.getLoggedFacebookUser()));
        userDAO.persistUserLogin(global.getLoggedLocalUser());

        SyncHelper syncHelper = new SyncHelper();
        database.close();
        syncHelper.sync(global.getLoggedLocalUser(), this);

    }

    private void redirect() {
        if (global.getLoggedFacebookUser() != null) {
            final Intent listTripsActivityIntent = new Intent(this,MainActivity.class);
            startActivity(listTripsActivityIntent);
        }
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {

        if(state.isOpened())
        {
            redirect();
        }
        else
        {
            profilePictureView.setProfileId(null);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
}
